package com.chinairi.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinairi.common.utils.IdGenerator;
import com.chinairi.common.utils.UserSession;
import com.chinairi.mall.controller.viewobject.ReceiveAddressVO;
import com.chinairi.mall.dao.GoodsShoppingCartMapper;
import com.chinairi.mall.error.BusinessException;
import com.chinairi.mall.error.EmBusinessError;
import com.chinairi.mall.model.*;
import com.chinairi.mall.service.IGoodsShoppingCartService;
import com.chinairi.mall.service.modelobject.GoodsShoppingCartMO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 购物车表 服务实现类
 * </p>
 *
 * @author gsl
 * @since 2020-09-26
 */
@Service
public class GoodsShoppingCartServiceImpl extends ServiceImpl<GoodsShoppingCartMapper, GoodsShoppingCart> implements IGoodsShoppingCartService {

    @Autowired
    private GoodsShoppingCartMapper goodsShoppingCartMapper;


    /**
     * 加入商品到购物车
     *
     * @param goodsShoppingCartList
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addShoppingCart(List<GoodsShoppingCart> goodsShoppingCartList) {
        if (goodsShoppingCartList.size() == 0) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "未选择商品");
        }
        QueryWrapper<GoodsShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PLAN_PURCHASE_ID", goodsShoppingCartList.get(0).getPlanPurchaseId()).eq("USER_ID",UserSession.getUserId());
        //查出来的结果集
        List<GoodsShoppingCart> goodsShoppingCartListResult = goodsShoppingCartMapper.selectList(queryWrapper);
        Map<String, GoodsShoppingCart> goodsShoppingCartMap = new HashMap<>(goodsShoppingCartListResult.size());
        //将查出来的结果集放入map中
        for (GoodsShoppingCart goodsShoppingCart : goodsShoppingCartListResult) {
            goodsShoppingCartMap.put(goodsShoppingCart.getCartGoodsDbid(), goodsShoppingCart);
        }
        for (GoodsShoppingCart goodsShoppingCart : goodsShoppingCartList) {
            if (goodsShoppingCartMap.get(goodsShoppingCart.getCartGoodsDbid()) != null) {
                goodsShoppingCartMap.get(goodsShoppingCart.getCartGoodsDbid()).setCartGoodsNumber(goodsShoppingCart.getCartGoodsNumber() + goodsShoppingCartMap.get(goodsShoppingCart.getCartGoodsDbid()).getCartGoodsNumber());
            } else {
                goodsShoppingCart.setScId(IdGenerator.UUID());
                goodsShoppingCart.setUserId(UserSession.getUserId());
                goodsShoppingCart.setCartAddTime(new Date());
                goodsShoppingCart.setCreateUser(UserSession.getUserId());
                goodsShoppingCart.setCreateTime(new Date());
                goodsShoppingCartListResult.add(goodsShoppingCart);
            }
        }
        goodsShoppingCartMapper.batchInsert(goodsShoppingCartListResult);
    }

    /**
     * 获取购物车中商品
     *
     * @return
     */
    @Override
    public HashMap<String, HashMap<String, List<GoodsShoppingCartMO>>> getCartGoods() {
        List<GoodsShoppingCartMO> goodsShoppingCartList = goodsShoppingCartMapper.selectByUserId(UserSession.getUserId());
        HashMap<String, HashMap<String, List<GoodsShoppingCartMO>>> outMap = new HashMap<>(16);
        for (GoodsShoppingCartMO goodsShoppingCartMO : goodsShoppingCartList) {
            String planPurchaseId = goodsShoppingCartMO.getPlanPurchaseId();
            String cartCsDbid = goodsShoppingCartMO.getCartCsDbid();
            if (outMap.containsKey(planPurchaseId)) {
                if (outMap.get(planPurchaseId).containsKey(cartCsDbid)) {
                    outMap.get(planPurchaseId).get(cartCsDbid).add(goodsShoppingCartMO);
                } else {
                    outMap.get(planPurchaseId).put(cartCsDbid, new ArrayList<>());
                    outMap.get(planPurchaseId).get(cartCsDbid).add(goodsShoppingCartMO);
                }
            } else {
                outMap.put(planPurchaseId, new HashMap<>(16));
                outMap.get(planPurchaseId).put(cartCsDbid, new ArrayList<>());
                outMap.get(planPurchaseId).get(cartCsDbid).add(goodsShoppingCartMO);
            }
        }
        return outMap;
    }

    /**
     * 修改购物车商品数量
     *
     * @param goodsShoppingCart
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateGoodsNumber(GoodsShoppingCart goodsShoppingCart) {
        goodsShoppingCartMapper.updateGoodsNumber(goodsShoppingCart.getScId(), goodsShoppingCart.getCartGoodsNumber());
    }

    /**
     * 获取购物车中的服务
     *
     * @return
     */
    @Override
    public HashMap<String, List<Object>> getCartService() {
        List<ProjectPlanPurchaseBuy> buyList = goodsShoppingCartMapper.selectBuy(UserSession.getUserId());
        List<ProjectPlanPurchaseTransport> transportList = goodsShoppingCartMapper.selectTransport(UserSession.getUserId());
        List<ProjectPlanPurchaseStorage> storageList = goodsShoppingCartMapper.selectStorage(UserSession.getUserId());
        List<ProjectPlanPurchaseQuality> qualityList = goodsShoppingCartMapper.selectQuality(UserSession.getUserId());
        List<ProjectPlanPurchaseSettle> settleList = goodsShoppingCartMapper.selectSettle(UserSession.getUserId());
        List<ProjectPlanPurchaseControl> controlList = goodsShoppingCartMapper.selectControl(UserSession.getUserId());

        HashMap<String, List<Object>> map = new HashMap<>(16);

        //放入购物车中的采购计划
        for (ProjectPlanPurchaseBuy buy : buyList) {
            map.put(buy.getPlanPurchaseId(), new ArrayList<>());
        }
        for (ProjectPlanPurchaseTransport transport : transportList) {
            map.put(transport.getPlanPurchaseId(), new ArrayList<>());
        }
        for (ProjectPlanPurchaseStorage storage : storageList) {
            map.put(storage.getPlanPurchaseId(), new ArrayList<>());
        }
        for (ProjectPlanPurchaseQuality quality : qualityList) {
            map.put(quality.getPlanPurchaseId(), new ArrayList<>());
        }
        for (ProjectPlanPurchaseSettle settle : settleList) {
            map.put(settle.getPlanPurchaseId(), new ArrayList<>());
        }
        for (ProjectPlanPurchaseControl control : controlList) {
            map.put(control.getPlanPurchaseId(), new ArrayList<>());
        }

        //追加
        for (ProjectPlanPurchaseBuy buy : buyList) {
            map.get(buy.getPlanPurchaseId()).add(buy);
        }
        for (ProjectPlanPurchaseTransport transport : transportList) {
            map.get(transport.getPlanPurchaseId()).add(transport);
        }
        for (ProjectPlanPurchaseStorage storage : storageList) {
            map.get(storage.getPlanPurchaseId()).add(storage);
        }
        for (ProjectPlanPurchaseQuality quality : qualityList) {
            map.get(quality.getPlanPurchaseId()).add(quality);
        }
        for (ProjectPlanPurchaseSettle settle : settleList) {
            map.get(settle.getPlanPurchaseId()).add(settle);
        }
        for (ProjectPlanPurchaseControl control : controlList) {
            map.get(control.getPlanPurchaseId()).add(control);
        }
        return map;
    }

    /**
     * 获取首页展示的购物车商品数量
     *
     * @return
     */
    @Override
    public Integer countGoodsNum() {
        return goodsShoppingCartMapper.selectCountGoodsNum(UserSession.getUserId());
    }

    /**
     * 根据采购计划ID获取收货地址
     *
     * @param planPurchaseId
     * @return
     */
    @Override
    public ReceiveAddressVO getReceiveAddress(String planPurchaseId) {
        return goodsShoppingCartMapper.getReceiveAddress(planPurchaseId);
    }

}
